package com.example.bootintegration.service;

import com.example.bootintegration.dao.test.entity.Student;
import com.example.bootintegration.dao.test.entity.StudentIdx;
import com.example.bootintegration.dao.test.repository.StudentRepositorytest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author Quintoy
 * @date 01/06/23
 */
@Component
public class TransactionTest {

    @Autowired
    private StudentRepositorytest studentRepositorytest;

    @Autowired
    private TransactionTest test;

    @Autowired
    @Qualifier("testJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    // @Transactional
    public void testJpa() {
        Optional<Student> studentRepositorytestById = studentRepositorytest.findById(new StudentIdx(1,1));
        Student student = studentRepositorytestById.get();
        // student.setAge(18110);

        // studentRepositorytest.updateStudent(1, 20);
        // studentRepositorytest.save(student);
        // Optional<Student> studentRepositorytestById1 = studentRepositorytest.findById(1);
        System.out.println("sss");
    }


    @Transactional
    public void updateStu() {
        throw new RuntimeException("test error");
    }


    @Transactional
    public void test_deleteAndSave() {
        StudentIdx studentIdx = new StudentIdx(1, 1);
        // Student student = studentRepositorytest.findById(studentIdx).get();
        Student student = studentRepositorytest.queryById(1);
        studentRepositorytest.deleteById(studentIdx);
        // studentIdx.setId2(2);

        Student target = new Student();
        BeanUtils.copyProperties(student, target);
        target.setStudentIdx(new StudentIdx(1, 1));
        // target.setStu_code("add");

        // student.setStudentIdx(studentIdx);
        studentRepositorytest.save(target);
    }

}